<?php
class guahaoController extends Zend_Controller_Action
{
    public function init ()
    {
        /* Initialize action controller here */
    }
    private function getDb () // 初始化数据库DB
    {
        $dbAdapter = Zend_Controller_Front::getInstance()->getParam('bootstrap')
            ->getPluginResource('db')
            ->getDbAdapter();
        return $dbAdapter;
    }
    public function indexAction ()
    {
        // action body
    }
    public function selectAction ()
    {
        $this->_helper->layout->disableLayout();
        // $a=$_POST['a'];
        // Zend_Debug::dump($a);
        $db = $this->getDb();
        $officeSort = $_GET['office'] * 1;
        $officeID = $this->_getParam('room');
        if (empty($officeID)) {
            $page = $this->_getParam('page');
            $page = $page ? (int) $page : 1;
            $numPerPage = 12;
            $select = new Zend_Db_Select($db);
            $select->from(array('p' => 'office', array('name', 'officeid')));
            if (empty($officeSort)) {
                $select->where('office_sort<>"null"');
            } else {
                $select->where('office_sort=?', $officeSort);
            }
            $sql = $select->__toString();
            $result = $db->fetchAll($sql);
            $paginator = Zend_Paginator::factory($result);
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage($numPerPage);
            $this->view->paginator = $paginator;
        } else {
            // echo $officeID;
            $selectDoctor = new Zend_Db_Select($db);
            $selectDoctor->from('office', '*');
            $selectDoctor->join('doctor', 'office.officeid=doctor.officeID', 
            '*');
            $selectDoctor->where('office.officeid=?', $officeID);
            $sqlDoctor = $selectDoctor->__toString();
            // Zend_Debug::dump($sqlDoctor);
            $this->view->doctor = $db->fetchAll($sqlDoctor);
        }
    }
    public function guahaolistAction ()
    {
        $this->_helper->layout->disableLayout();
        $doctorID = $_GET['doctorID'];
        // echo $doctorID;
        $db = $this->getDb();
        $select = new Zend_Db_Select($db);
        $select->from('doctordate', 'czsj')->where('ysbm=?', $doctorID);
        $this->view->ysbm = $doctorID;
        // Zend_Debug::dump($select);
        $sql = $select->__toString();
        // echo $sql;
        $result = $db->fetchOne($sql);
        // echo $result;
        $binaryData = decbin($result); // 十进制转成二进制
        $this->view->b = $binaryData;
    }
    public function fastregisterAction ()
    { // 挂号信息显示
        $this->_helper->layout->disableLayout();
        $username = Zend_Auth::getInstance()->getIdentity();
        if (empty($username)) {
            echo $this->_redirect('guahao/userwrong'); // 如果用户名非法返回错误页面
        } else {
            $date = $_GET['date'];
            // //
            $this->view->date = $date; // 显示日期
            $ysbm = $_GET['ysbm'];
            $weekday = $_GET['weekday'];
            // // echo $weekday;
            $this->view->weekday = $weekday; // 显示星期
            $this->view->ysbm = $ysbm; // 医生编码
            $db = $this->getDb();
            $select = new Zend_Db_Select($db);
            $select->from('doctor', 'doctorName')->where('doctorID=?', $ysbm);
            // // Zend_Debug::dump($select);
            $sql = $select->__toString();
            // echo $sql;
            $result = $db->fetchOne($sql);
            $this->view->doctor = $result; // 医生姓名
            $selectUser = new Zend_Db_Select($db);
            $selectUser->from('user', 'userID')->where('username=?', $username);
            $sqlUser = $selectUser->__toString();
            $resultUserId = $db->fetchOne($sqlUser);
            $this->view->userID = $resultUserId; // 患者身份证号
        }
    }
    public function userwrongAction ()
    {
        $this->_helper->layout->disableLayout();
    }
    public function checkAction ()
    {
        $this->_helper->layout->disableLayout();
        $guahaoParams = $this->_request->getParams();
        $ysbm = $_POST['ysbm'];
        $ghdate = $_POST['date'];
        $weekday = $_POST['weekday'];
        $userID = $_POST['userID'];
        $db = $this->getDb();
        $select1 = new Zend_Db_Select($db);
        $select1->from('ghmx', 'userID')
            ->where('ysbm=?', $ysbm)
            ->where('ghdate=?', $ghdate);
        $sql1 = $select1->__toString();
        $result1 = $db->fetchAll($sql1);
        $guahaoNum = count($result1); // 得到现有挂号数量
                                      // echo $guahaoNum;
                                      // // echo $result['userID'];
        $select2 = new Zend_Db_Select($db);
        $select2->from('doctornum', 'shuliang')
            ->where('ysbm=?', $ysbm)
            ->where('weekday=?', $weekday);
        $sql2 = $select2->__toString();
        $result2 = $db->fetchRow($sql2);
        $NUM = $result2['shuliang'];
        // echo $NUM;
        try {
            if ($guahaoNum < $NUM) {
                $table = new Application_Model_ghmx();
                $row = array('ysbm' => $ysbm, 'userID' => $userID, 
                'ghdate' => $ghdate);
                $table->insert($row);
                echo '挂号成功';
            } else {
                echo '挂号失败';
            }
        } catch (Exception $e) {
            echo '挂号内容有误请，重新挂号';
        }
    }
}
